package com.ruoyi.common.utils.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class MySQLUtil {

    // 数据库连接信息
    private static final String URL = "jdbc:mysql://172.30.30.141:3306/haochengjixiao"; // 替换为你的数据库名
    private static final String USER = "jixiao"; // 替换为你的用户名
    private static final String PASSWORD = "haocheng0509"; // 替换为你的密码

    /**
     * 获取数据库连接
     *
     * @return Connection 对象
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            // 加载 MySQL 驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立连接
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            System.err.println("MySQL 驱动未找到！");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("数据库连接失败！");
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 读取表 btype 的数据
     */
    public static void readBTypeTable() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // 获取连接
            connection = getConnection();

            // SQL 查询语句
            String sql = "SELECT * FROM btype";

            // 创建 PreparedStatement 对象
            preparedStatement = connection.prepareStatement(sql);

            // 执行查询
            resultSet = preparedStatement.executeQuery();
            // 处理结果集
            while (resultSet.next()) {
                String btypeid = resultSet.getString("btypeid");
                String name = resultSet.getString("bfullname");
                // 输出数据
                System.out.println("ID: " + btypeid + ", Name: " + name);
            }
        } catch (SQLException e) {
            System.err.println("读取表 btype 时出错！");
            e.printStackTrace();
        } finally {
            // 关闭资源
            closeResources(connection, preparedStatement, resultSet);
        }
    }

    /**
     * 关闭数据库资源
     *
     * @param connection        数据库连接
     * @param preparedStatement PreparedStatement 对象
     * @param resultSet         ResultSet 对象
     */
    private static void closeResources(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            System.err.println("关闭资源时出错！");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 调用方法读取表 btype
        readBTypeTable();
    }
}